import { Component, OnInit } from '@angular/core';
import { RECORD_TYPE_OPTION } from '../../../constant/dictionary';
import { MyModalHelper } from '@core/my-modal.helper';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { IntegralRecordService } from '@core/routes-services/integral-record.service';
import { myValidNull } from '@core/util/string-utils';
import { CompanyIntegralViewComponent } from './integral-view/integral-view.component';

@Component({
  selector: 'app-company-integral-list',
  templateUrl: './integral-list.component.html',
})
export class CompanyIntegralListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  type: any ; //1-转入，2-转出 3-自购奖励 4 直接销售奖励 5 间接销售奖励 6 业务员奖励 7 渠道商奖金 8 门店|代理奖励
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  recordTypeOption:any [] = RECORD_TYPE_OPTION
  constructor(private message: NzMessageService,
              private router: Router,
              private integralRecordService: IntegralRecordService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }


  initPage() {
    this.isLoading = true;
    this.integralRecordService.pagination(this.page, this.keyword, this.type).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(CompanyIntegralViewComponent, { entity: bak }, 'lg', {
      nzTitle: '积分记录详情',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
}
